﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace TaiwuZD
{
    public class GameAssets : BindableBase
    {

        //public static GameAssets Inst { get; private set; } = new GameAssets();

        /// <summary>
        /// 角色姓氏
        /// </summary>
        public static Dictionary<int, string> ActorSurNames { get; private set; }

        /// <summary>
        /// 特性
        /// </summary>
        public static ActorFeature[] ActorFeatures { get; private set; }


        public static ReadOnlyDictionary<int, TwItemTemplate> TwItems { get;private set; }

        public static ReadOnlyDictionary<int,TwItemPower> TwItemPowers { get; private set; }

        public static ReadOnlyDictionary<string, string> TwID { get; private set; } = new ReadOnlyDictionary<string, string>();

        public static ReadOnlyDictionary<int,TwBuilding> TwBuildings { get; private set; }

        /// <summary>
        /// 个人喜爱集
        /// </summary>
        public static string[] TwPersonalInterests { get; private set; } =  { "无","针匣","对刺","暗器","箫笛","掌套","短杵","拂尘","长鞭","剑","刀","长兵","瑶琴","宝物","冠饰","鞋子","护甲","衣着","代步","促织","图纸","书籍","工具","食材","木材","金铁","宝石","织物","毒物","药材","毒药","丹药","杂物","蛰罐","素食","荤食","神兵","酒","机关","毒器","令符","茶" };
         

        public static TwItemTypes TwItemTypes { get; private set; }

        public static ReadOnlyDictionary<int, TwGongFa>  TwGongFas { get; private set; }

        public static ReadOnlyDictionary<int, TwSkill> TwSkills { get; private set; }


        //public static int[] TwItemTypesL1 { get; private set; }
        //public static int[] TwItemTypesL2 { get; private set; }
        //public static int[] TwItemTypesL3 { get; private set; }

        static GameAssets()
        {
           LoadAssets();
        }


        public class ReadOnlyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
        {
            private new void Add(TKey key, TValue value)
            {
                base.Add(key, value);
            }

            protected new void Clear()
            {
                base.Clear();
            }

            protected new bool Remove(TKey key)
            {
                return base.Remove(key);
            }

            public new TValue this[TKey key]
            {
                get
                {
                    if (Keys.Contains(key))
                        return base[key];
                    else
                        return default(TValue);
                }
                set
                {
                    if (Keys.Contains(key))
                        base[key] = value;
                    else
                        Add(key, value);
                }
            }
        }


        public static void LoadAssets()
        {
            TwID = LoadResource<ReadOnlyDictionary<string, string>>(nameof(TwID));
            ActorSurNames = LoadResource<Dictionary<int, string>>(nameof(ActorSurNames));  
            TwSkills = LoadResource<ReadOnlyDictionary<int, TwSkill>>(nameof(TwSkills));

            LoadFeatures();
            LoadTwItemTypes();
            LoadTwItem();
            LoadTwItemPower();
            LoadTwBuilding();
            LoadTwGongFa();
        }






        /// <summary>
        /// 加载资源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propname"></param>
        /// <returns></returns>
        private static T LoadResource<T>(string propname)
        {
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    return JsonConvert.DeserializeObject<T>(tmp);
                }
            }
            return default(T);
        }


        /// <summary>
        /// 加载特性
        /// </summary>
        private static void LoadFeatures()
        {
            var propname = "ActorFeature";
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    var jtoken = JsonConvert.DeserializeObject(tmp) as JToken;

                    List<ActorFeature> list = new List<ActorFeature>();
                    foreach (JProperty feature in jtoken.Skip(1))
                    {
                        var af = ActorFeature.LoadFromJson(feature);

                        list.Add(af);
                    }
                    ActorFeatures = list.ToArray();
                }
            }
        }

        private static void LoadTwItem()
        {
            var propname = "Item";
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    var jtoken = JsonConvert.DeserializeObject(tmp) as JToken;

                    TwItems = new ReadOnlyDictionary<int, TwItemTemplate>();
                    foreach (JProperty pp in jtoken)
                    {
                        if (pp.Value["4"].ToString() == "0")
                            continue;

                        var af = TwItemTemplate.LoadFromJson(pp);

                        TwItems.Add(af.Id, af);
                    } 
                }
            }
        }

        private static void LoadTwItemPower()
        {
            var propname = "ItemPower";
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    var jtoken = JsonConvert.DeserializeObject(tmp) as JToken;

                    TwItemPowers = new ReadOnlyDictionary<int, TwItemPower>();
                    foreach (JProperty pp in jtoken)
                    {
                        
                        var af = TwObject.LoadFromJson< TwItemPower>(pp);

                        TwItemPowers.Add(af.Id,af);
                    } 
                }
            }
        }

        private static void LoadTwBuilding()
        {
            var propname = nameof(TwBuilding);
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    var jtoken = JsonConvert.DeserializeObject(tmp) as JToken;

                    TwBuildings = new ReadOnlyDictionary<int, TwBuilding>();
                    foreach (JProperty pp in jtoken)
                    {

                        var af = TwObject.LoadFromJson<TwBuilding>(pp);

                        TwBuildings.Add(af.Id, af);
                    }
                }
            }
        }

        private static void LoadTwItemTypes()
        {

            var propname = nameof(TwItemTypes);
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();

                    TwItemTypes = JsonConvert.DeserializeObject<TwItemTypes>(tmp);// new TwItemTypes(tmp);
                     
                } 
            }


        }

        private static void LoadTwGongFa()
        {
            var propname = nameof(TwGongFas);
            var stream = Application.GetResourceStream(new Uri($"/json/{propname}.json", UriKind.Relative));

            if (stream != null && stream.Stream != null && stream.Stream.CanRead)
            {
                using (var sr = new StreamReader(stream.Stream))
                {
                    var tmp = sr.ReadToEnd();
                    var jtoken = JsonConvert.DeserializeObject(tmp) as JToken;

                    TwGongFas = new ReadOnlyDictionary<int, TwGongFa>();
                    foreach (JProperty pp in jtoken)
                    {                      
                        var af = TwObject.LoadFromJson<TwGongFa>(pp);

                        if (af.Id < 160000)
                            TwGongFas.Add(af.Id, af);
                    }
                }
            }
        }
    }
}
